﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SaleOnlineSystem.Admin.Controls
{
    public partial class ListShowProduct : System.Web.UI.UserControl
    {
        public DataTable dt;
        public int idproductcolor;
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!Page.IsPostBack)
            {
                //ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "vTip", "jQuery(document).ready(function($){vtip();})", true);
                //ScriptManager.RegisterStartupScript(UpdatePanel2, UpdatePanel2.GetType(), "vTip", "jQuery(document).ready(function($){vtip();})", true);
                //ScriptManager.RegisterStartupScript(UpdatePanel3, UpdatePanel3.GetType(), "vTip", "jQuery(document).ready(function($){vtip();})", true);
                //ScriptManager.RegisterStartupScript(UpdatePanel4, UpdatePanel4.GetType(), "vTip", "jQuery(document).ready(function($){vtip();})", true);
                //ScriptManager.RegisterStartupScript(UpdatePanel5, UpdatePanel5.GetType(), "vTip", "jQuery(document).ready(function($){vtip();})", true);
                // Them du lieu vao cat 1
                dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Category", "*", "Category_ID2", 0, "selectwhereid");
                ListItem lt = new ListItem("Chon danh muc", "-1");
                Cate1.Items.Add(lt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                    Cate1.Items.Add(lt);
                }
                Cate1.SelectedIndex = 0;

                DataTable dt1 = null;

                Repeater1.DataSource = dt1;
                Repeater1.DataBind();
            }
        }

        protected void Cate1_SelectedIndexChanged(object sender, EventArgs e)
        {

            Cate2.Items.Clear();
            Cate3.Items.Clear();

            int index = Cate1.SelectedIndex;
            if (index > 0)
            {
                int id = Convert.ToInt32(Cate1.SelectedValue);
                dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Category", "*", "Category_ID2", id, "selectwhereid");
                ListItem lt = new ListItem("Chon danh muc", "-1");
                Cate2.Items.Add(lt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                    Cate2.Items.Add(lt);
                }

                dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByGroupID("tbl_Product", "*", "Category_ID3", id.ToString());
                rpt_Sanpham.DataSource = dt;
                rpt_Sanpham.DataBind();

                if (Cate2.Items.Count <= 1)
                {
                    Repeater1.DataSource = DAL.DCollector.DBCollector.Instance.SelectSQL("Select * from tbl_ProductColor,tbl_Color,tbl_Product Where tbl_ProductColor.Color_ID = tbl_Color.Color_ID AND tbl_ProductColor.Product_ID = tbl_Product.Product_ID AND tbl_Product.Category_ID1="+Cate1.SelectedValue);
                    Repeater1.DataBind();
                }
            }

        }

        protected void Cate2_SelectedIndexChanged(object sender, EventArgs e)
        {
            Cate3.Items.Clear();
            int index = Cate2.SelectedIndex;
            if (index > 0)
            {
                int id = Convert.ToInt32(Cate2.SelectedValue);
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Category", "*", "Category_ID2", id, "selectwhereid");
                ListItem lt = new ListItem("Chon danh muc", "-1");
                Cate3.Items.Add(lt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                    Cate3.Items.Add(lt);
                }
                dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByGroupID("tbl_Product", "*", "Category_ID3", id.ToString());
                rpt_Sanpham.DataSource = dt;
                rpt_Sanpham.DataBind();
                if (Cate3.Items.Count <= 1)
                {
                    Repeater1.DataSource = DAL.DCollector.DBCollector.Instance.SelectSQL("Select * from tbl_ProductColor,tbl_Color,tbl_Product Where tbl_ProductColor.Color_ID = tbl_Color.Color_ID AND tbl_ProductColor.Product_ID = tbl_Product.Product_ID AND tbl_Product.Category_ID2"+Cate2.SelectedValue);
                    Repeater1.DataBind();
                }
            }
        }

        protected void Cate3_SelectedIndexChanged(object sender, EventArgs e)
        {

            int id = Convert.ToInt32(Cate3.SelectedValue);
            dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByGroupID("tbl_Product", "*", "Category_ID3", id.ToString());
            rpt_Sanpham.DataSource = dt;
            rpt_Sanpham.DataBind();

            Repeater1.DataSource = DAL.DCollector.DBCollector.Instance.SelectSQL("Select * from tbl_ProductColor,tbl_Color,tbl_Product Where tbl_ProductColor.Color_ID = tbl_Color.Color_ID AND tbl_ProductColor.Product_ID = tbl_Product.Product_ID  AND tbl_Product.Category_ID3="+Cate3.SelectedValue);
            Repeater1.DataBind();

        }

        //protected void Cate1_TextChanged(object sender, EventArgs e)
        //{
        //    Cate2.Items.Clear();
        //    Cate3.Items.Clear();

        //    int index = Cate1.SelectedIndex;
        //    if (index > 0)
        //    {
        //        int id = Convert.ToInt32(Cate1.SelectedValue);
        //        dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Category", "*", "Category_ID2", id, "selectwhereid");
        //        ListItem lt = new ListItem("Chon danh muc", "-1");
        //        Cate2.Items.Add(lt);
        //        for (int i = 0; i < dt.Rows.Count; i++)
        //        {
        //            lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
        //            Cate2.Items.Add(lt);
        //        }
        //        dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByGroupID("tbl_Product", "*", "Category_ID3", id.ToString());
        //        rpt_Sanpham.DataSource = dt;
        //        rpt_Sanpham.DataBind();
        //        Repeater1.DataSource = DAL.DCollector.DBCollector.Instance.SelectSQL("Select * from tbl_ProductColor,tbl_Color,tbl_Product Where tbl_ProductColor.Color_ID = tbl_Color.Color_ID AND tbl_ProductColor.Product_ID = tbl_Product.Product_ID");
        //        Repeater1.DataBind();

        //    }
        //}

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {
                // Xoa
                int id = Convert.ToInt32(e.CommandArgument.ToString());
                dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_ProductColor", "*", "ProductColor_ID", id, "selectwhereid");
                string bigimage = dt.Rows[0]["Product_ImageColor"].ToString();
                string smallimage = dt.Rows[0]["Product_MultiImage"].ToString();
                string[] splitimagesmall = smallimage.Split(',');
                File.Delete(MapPath(bigimage));
                File.Delete(MapPath(splitimagesmall[0]));
                File.Delete(MapPath(splitimagesmall[1]));
                File.Delete(MapPath(splitimagesmall[2]));
                DAL.DCollector.DBCollector.Instance.StoredProcedureDeleteByRows("tbl_ProductSize", "ProductColor_ID", id.ToString());
                DAL.DCollector.DBCollector.Instance.StoredProcedureDeleteByRows("tbl_ProductColor", "ProductColor_ID", id.ToString());
                Response.Redirect("/Admin/Index.aspx?panel=1&sub=1");
            }
            if (e.CommandName == "khongsize")
            {
                idproductcolor = Convert.ToInt32(e.CommandArgument.ToString());
                DataTable dt = DAL.DCollector.DBCollector.Instance.SelectSQL("Select Count(*) as result from tbl_ProductSize Where ProductCOlor_ID =" + idproductcolor);
                int count = Convert.ToInt32(dt.Rows[0]["result"].ToString());
                if (count == 0)
                {
                    int nextid = Convert.ToInt32(DAL.DCollector.DBCollector.Instance.getNextID("tbl_ProductSize", "ProductSize_ID"));
                    int idproductsize = nextid + 1;

                    string value2 = "" + idproductsize + "," + idproductcolor + "," + 1 + "";
                    List<string> values2 = new List<string>();
                    values2.Add(value2);
                    DAL.DCollector.DBCollector.Instance.StoredProcedureInsertByRows("tbl_ProductSize", "insertbyrows", "*", values2, false);
                    values2.Clear();
                    lblCheck.Text = "Sản phẩm của bạn đã được cập nhật “Không Size”";
                }
                else
                {
                    lblCheck.Text = "Sản phẩm của bạn đã có Size. Không thể cập nhật “Không Size” . Vui lòng Click “Cập nhật Size” để thay đổi Size theo ý muốn";                  
                }

            }
        }

        protected void rpt_Sanpham_ItemCommand1(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {
                // Xoa
                int id = Convert.ToInt32(e.CommandArgument.ToString());
                dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Product", "*", "Product_ID", id, "selectwhereid");
                string imageavatar = dt.Rows[0]["Product_Image"].ToString();
                File.Delete(MapPath(imageavatar));
                dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_ProductColor", "*", "Product_ID", id, "selectwhereid");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string bigimage = dt.Rows[i]["Product_ImageColor"].ToString();
                    string smallimage = dt.Rows[i]["Product_MultiImage"].ToString();
                    string[] splitimagesmall = smallimage.Split(',');
                    File.Delete(MapPath(bigimage));
                    File.Delete(MapPath(splitimagesmall[0]));
                    File.Delete(MapPath(splitimagesmall[1]));
                    File.Delete(MapPath(splitimagesmall[2]));
                }

                DAL.DCollector.DBCollector.Instance.CallStored("callstored", "DELETE FROM tbl_ProductSize Where ProductSize_ID IN( SELECT ProductSize_ID FROM tbl_Product , tbl_ProductColor ,tbl_ProductSize , tbl_Size, tbl_Color where tbl_Product.Product_ID = tbl_ProductColor.Product_ID AND tbl_ProductSize.ProductColor_ID= tbl_ProductColor.ProductColor_ID AND tbl_ProductSize.Size_ID = tbl_Size.Size_ID AND tbl_ProductColor.Color_ID = tbl_Color.Color_ID AND tbl_Product.Product_ID = " + id + ")", false);
                DAL.DCollector.DBCollector.Instance.StoredProcedureDeleteByRows("tbl_ProductColor", "Product_ID", id.ToString());
                //DAL.DCollector.DBCollector.Instance.StoredProcedureDeleteByRows("tbl_Product", "Product_ID", id.ToString());
                DAL.DCollector.DBCollector.Instance.StoredProcedureDeleteByRowProduct("tbl_Product", id, "deleterowproduct");
                Response.Redirect("/Admin/Index.aspx?panel=1&sub=1");

            }
        }
    }
}